﻿using System;
using System.Collections.Generic;
using System.Windows;
using Facebook;
using Microsoft.Phone.Controls;

namespace FBEventMaker
{
    public partial class Page1 : PhoneApplicationPage
    {
        private const string sAppId = "338888456164855";  // <-- owner of the app        

        private const string sAppSecret = "78e55393974c5aa6a67f54728feeb85d"; // app's own id        

        /// <summary>
        /// Extended permissions is a comma separated list of permissions to ask the user.
        /// </summary>
        /// <remarks>
        /// For extensive list of available extended permissions refer to 
        /// https://developers.facebook.com/docs/reference/api/permissions/
        /// </remarks>
        private const string ExtendedPermissions = "user_about_me,publish_stream,offline_access,create_event,read_stream";

        private readonly Uri sLoginURI;
        public Page1()
        {
            InitializeComponent();
            var oauthClient = new FacebookOAuthClient();
            oauthClient.AppId = sAppId;

            var loginParameters = new Dictionary<string, object> 
                    {
                        //July 12 migration is disabled
                        { "display", "touch" }
                    };   
            loginParameters["response_type"] = "token";

            // add the 'scope' parameter only if we have extendedPermissions.
            if (!string.IsNullOrEmpty(ExtendedPermissions))
            {
                // A comma-delimited list of permissions
                loginParameters["scope"] = ExtendedPermissions;
            }

            // when the Form is loaded navigate to the login url.
            sLoginURI = oauthClient.GetLoginUrl(loginParameters);

         //   MessageBox.Show("LoginURI: " + sLoginURI.OriginalString);
           
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            mFBLoginBrowser.Navigate(sLoginURI);
        }

        private void mFBLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            // whenever the browser navigates to a new url, try parsing the url
            // the url may be the result of OAuth 2.0 authentication.
            FacebookOAuthResult oauthResult;

            if (FacebookOAuthResult.TryParse(e.Uri, out oauthResult))
            {
                // The url is the result of OAuth 2.0 authentication.
                if (oauthResult.IsSuccess)
                {
                    var oauthClient = new FacebookOAuthClient { AppId = sAppId, AppSecret = sAppSecret };
                   
                     //  var code = oauthResult.Code;
                    var accessToken = oauthResult.AccessToken;
                    if (!String.IsNullOrWhiteSpace("/Facebook/FacebookInfoPage.xaml"))
                    {
                        NavigationService.Navigate(new Uri("/Facebook/FacebookInfoPage.xaml?access_token=" + accessToken, UriKind.Relative));
                        //  mFBLoginBrowser.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
                else
                {
                    // the user clicked don't allow or some other error occurred.
                    MessageBox.Show(oauthResult.ErrorDescription);
                }
            }
            else
            {
               // MessageBox.Show("The url is NOT the result of OAuth 2.0 authentication");
            }
        }      
    }
}